package com.jsyxdl.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.extra.spring.SpringUtil;
import com.alibaba.fastjson.JSON;
import com.google.common.collect.Lists;
import com.hustcad.plm.basic.util.TyplmLoggerUtil;
import com.hustcad.plm.pdm.common.util.CommonUtil;
import com.hustcad.plm.pdm.iba.model.dto.IBASaveParamDTO;
import com.hustcad.plm.pdm.iba.service.TyplmCommonAttributeService;
import com.hustcad.plm.pdm.iteration.service.TyplmCommonIterateService;
import com.hustcad.plm.pdm.partbom.dto.TyplmUpdateLinkIbaDTO;
import com.hustcad.plm.pdm.partbom.enums.PartBomErrorCodeEnum;
import com.hustcad.plm.pdm.partbom.service.TyplmPartAlterLinkSaveService;
import com.hustcad.plm.pdm.partbom.service.impl.partalternate.TyplmPdmPartAlternateServiceImpl;
import com.hustcad.plm.pdm.type.model.vo.TypeVueVO;
import com.hustcad.plm.pdm.type.service.TyplmTypeService;
import com.ty.basic.entity.CTyEntityBaseDO;
import com.ty.basic.entity.part.PartAlterLinkDO;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigInteger;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

@Service
@Primary
public class ExtendtyplmPdmPartAlternateServiceImpl extends TyplmPdmPartAlternateServiceImpl {
    private static final Logger log = LoggerFactory.getLogger(ExtendtyplmPdmPartAlternateServiceImpl.class);

    @Transactional(propagation = Propagation.REQUIRES_NEW)
    @Override
    public boolean batchUpdatePartAlterLinkIba(List<TyplmUpdateLinkIbaDTO> typlmUpdateLinkIbaList) {

        TyplmPartAlterLinkSaveService typlmPartAlterLinkSaveService= SpringUtil.getBean(TyplmPartAlterLinkSaveService.class);
        TyplmTypeService typlmTypeService=SpringUtil.getBean(TyplmTypeService.class);
        TyplmCommonIterateService typlmCommonIterateService=SpringUtil.getBean(TyplmCommonIterateService.class);
        TyplmCommonAttributeService typlmCommonAttributeService=SpringUtil.getBean(TyplmCommonAttributeService.class);
        if (CollUtil.isEmpty(typlmUpdateLinkIbaList)) {
            return Boolean.FALSE;
        }
        else {
            TyplmLoggerUtil.debug(log, () -> {
                return "partAlterLinkVOList  ==> 批量修改部件全局替代关联关系上扩展属性,接口入参信息 =【{}】";
            }, () -> {
                return new Object[]{JSON.toJSONString(typlmUpdateLinkIbaList)};
            });
            List<BigInteger> partAlterLinkOidList = (List)typlmUpdateLinkIbaList.stream().filter((obj) -> {
                return StringUtils.isNotBlank(obj.getLinkId());
            }).map((obj) -> {
                return CommonUtil.stringToBigInteger(obj.getLinkId());
            }).collect(Collectors.toList());
            List<PartAlterLinkDO> linkList = typlmPartAlterLinkSaveService.queryPartAlterLinkDOByIds(partAlterLinkOidList);
            List<IBASaveParamDTO> objWithIbaList = new ArrayList();
            BigInteger typeOid = null;
            List<TypeVueVO> typeVueVOS = typlmTypeService.queryTypeByNameList(Lists.newArrayList(new String[]{"ty.inteplm.part.CTyPartAlterLink"}));
            if (CollUtil.isNotEmpty(typeVueVOS)) {
                typeOid = ((TypeVueVO)typeVueVOS.get(0)).getOid();
            }

            if (ObjectUtil.isEmpty(typeOid)) {
                throw PartBomErrorCodeEnum.PARTBOM_OBJTYPE_NOT_EXIST.getException(new Object[]{"ty.inteplm.part.CTyPartAlterLink"});
            } else {
                Map<BigInteger, PartAlterLinkDO> partAlterLinkMap = (Map)linkList.stream().collect(Collectors.toMap(PartAlterLinkDO::getOid, Function.identity(), (k1, k2) -> {
                    return k1;
                }));
                Iterator var8 = typlmUpdateLinkIbaList.iterator();

                while(var8.hasNext()) {
                    TyplmUpdateLinkIbaDTO typlmUpdateLinkIbaDTO = (TyplmUpdateLinkIbaDTO)var8.next();
                    BigInteger linkOid = CommonUtil.stringToBigInteger(typlmUpdateLinkIbaDTO.getLinkId());
                    if (CollUtil.isNotEmpty(typlmUpdateLinkIbaDTO.getIbaAttributeMap())) {
                        typlmCommonIterateService.parseIBASaveParamDTOIbaDtoList((CTyEntityBaseDO)partAlterLinkMap.get(linkOid), typlmUpdateLinkIbaDTO.getIbaAttributeMap(), objWithIbaList, Boolean.FALSE, typeOid);
                    }
                }

                List<List<IBASaveParamDTO>> pieceList = CommonUtil.subObjectList(objWithIbaList, 200);
                Iterator var13 = pieceList.iterator();

                while(var13.hasNext()) {
                    List<IBASaveParamDTO> ibadtoList = (List)var13.next();
                    TyplmLoggerUtil.debug(log, () -> {
                        return "batchUpdatePartAlterLinkIBA  ==> 更新扩展属性集合ibadtoList =【{}】";
                    }, () -> {
                        return new Object[]{JSON.toJSONString(ibadtoList)};
                    });
                    typlmCommonAttributeService.batchSaveAttributeValue(ibadtoList);
                }

                return Boolean.TRUE;
            }
        }
    }

}
